<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\service\DesEncrypt;
use app\service\IyunBinkService;
use app\admin\model\Order;
use app\admin\model\UserRelation;
use think\facade\Log;
use think\facade\Queue;
/**
 *I云服回调
 */
class IyunCallBackNotify extends Api
{
  protected $noNeedLogin = ['*'];
  protected $noNeedRight = ['*'];
  public $JobClassName = 'app\job\EquityJob';
  /**
   * 订单回调
   *
   * @return string
   */
  public function Order()
  {
    $data = json_decode(file_get_contents('php://input'), true);
    Log::info("==I云服订单回调==" . json_encode($data));
    $list = $data;
    if ($list['encryFlag'] == 'Y') {
      $list = IyunBinkService::RSA_openssl($list["data"], 'decode');
      $list=json_decode($list,true);
    }
    Log::info("==I云服订单回调==" . json_encode($list));
    $orderModel = new Order();
    $orderFind = $orderModel->getFindOrderId($list['orderId'], 2);
    if ($orderFind) {
      //修改
      $orderFind->orderStatus = $list['orderStatus'];
      $orderFind->updatetime = time();
      $orderFind->status = 1;
      $JobQueueName = "";
      $jobData = ['ts' => time(),'order_id'=>$orderFind->id];
      Queue::later(10, $this->JobClassName, $jobData, $JobQueueName);
      $orderFind->save();
      return json_encode(['code' => 0, 'msg' => '成功']);
    } else {
      $UserRelationModel = new UserRelation();
      $user = $UserRelationModel->where('t_id', $list['uniqueId'])->find();
      if ($user) {
        $arr['user_id'] = $user->user_id;
      }
      //新增
      $arr = [
        'order_on' => $list['orderNo'], //订单号
        // 'policy_no'=>$list['policy_no'],//保单号
        'ware_id' => $list['wareID'], //产品号id
        'product_id' => $list['productId'],
        'ware_name' => $list['wareName'],
        'product_name' => $list['productName'],
        'orderId' => $list['orderId'],
        'mechanId' => $list['partner'], //渠道商id
        'commission_owner' => $list['uniqueId'], //用户账号id
        'guaranteeTerm' => isset($list['effectiveTime']) ? $list['effectiveTime'] . '-' . $list['expireTime'] : '',
        'guaranteeType' => $list['insureCode'],
        'pay_freq' => $list['payFreq'], //缴费方式
        // 'payment'=>$list['payment'],
        'insuredersJson' => isset($list['insurants']) ?json_encode( $list['insurants'] ): '', //被保人
        'benefitInfo' => isset($list['applicant']) ? json_encode($list['applicant']) : '', //投保人
        // 'payPerm'=>$list['payPerm'],
        'premium' => isset($list['premium']) ? $list['premium'] : '', //保费
        'totalPremium' => isset($list['premium']) ? $list['premium'] : '', //总保费
        'totalInsured' => isset($list['amount']) ? $list['amount'] : '', //保额
        // 'expireTime'=>$list['payTime'],
        'orderStatus' => $list['orderStatus'],
        'expires' => isset($list['expires']) ? $list['expires'] : '', //保障期间
        // 'effective_date'=>$list['waverLimit'],//结算犹豫期
        'insure_data' => isset($list['createTime']) ? $list['createTime'] : '', //承保时间
        'pay' => isset($list['pay']) ? $list['pay'] : '', //交费期间
        'type' => '2',
        'status' => '0',
        'createtime' => time()
      ];
      
      $ml = $orderModel->create($arr);
      Log::info("==I云服订单回调==" . json_encode($ml));
      if ($ml) {
       
        return json_encode(['code' => 0, 'msg' => '成功']);
      }
      return json_encode(['code' => 1, 'msg' => '失败']);
    }
  }
  /**
   * 保单通知回调
   *
   * @return string
   */
  public function policyOrderNotify()
  {
    $data = json_decode(file_get_contents('php://input'), true);
    Log::info("==I云服保单通知回调==" . json_encode($data));
    $list = $data;
    if ($list['encryFlag'] == 'Y') {
      $list = IyunBinkService::RSA_openssl($list["data"], 'decode');
      $list=json_decode($list,true);
    }
    $orderModel = new Order();
    $orderFind = $orderModel->getFindpolicyNo($list['policyNo'], 2);
    if ($orderFind) {
      //修改
      $orderFind->orderStatus = $list['policyStatus'];
      $orderFind->updatetime = time();
      $orderFind->save();
      return json_encode(['code' => 0, 'msg' => '成功']);
    } else {
      $UserRelationModel = new UserRelation();
      $id=isset($list['uniqueId']) ? $list['uniqueId'] : $list['accountId'];
      $user = $UserRelationModel->where('t_id',  $id)->find();
      if ($user) {
        $arr['user_id'] = $user->user_id;
      }
      //新增
      $arr = [
        'order_on' => $list['orderNo'], //订单号
        'policy_no' => $list['policy_no'], //保单号
        'ware_id' => isset($list['wareID']) ? $list['wareID'] : '', //产品号id
        'product_id' => isset($list['productId']) ? $list['productId'] : '',
        'ware_name' => isset($list['wareName']) ? $list['wareName'] : '',
        'product_name' => $list['productName'],
        // 'orderId'=>$list['orderId'],
        'mechanId' => $list['partner'], //渠道商id
        'commission_owner' => isset($list['uniqueId']) ? $list['uniqueId'] : $list['accountId'], //用户账号id
        'guaranteeTerm' => $list['effectiveTime'] . '-' . $list['expireTime'],
        'guaranteeType' => $list['insureCode'],
        'pay_freq' => $list['payFreq'], //缴费方式
        // 'payment'=>$list['payment'],
        'insuredersJson' => isset($list['insurants']) ?json_encode( $list['insurants'] ): '', //被保人
        'benefitInfo' => isset($list['applicant']) ? json_encode($list['applicant']) : '', //投保人
        // 'payPerm'=>$list['payPerm'],
        'premium' => $list['premium'], //保费
        'totalPremium' => $list['premium'], //总保费
        'totalInsured' => $list['amount'], //保额
        // 'expireTime'=>$list['payTime'],
        'orderStatus' => $list['policyStatus'], //保单状态
        'expires' => $list['insure'], //保障期间
        'effective_date' => $list['waverLimit'], //结算犹豫期
        'insure_data' => $list['insureTime'], //承保时间
        'returnVisitStatus' => $list['receiptType'],
        'payment' => $list['payment'], //交费期间
        'pay' => isset($list['pay']) ? $list['pay'] : '',
        'type' => '2',
        'status' => '0',
        'createtime' => time()
      ];
      $ml = $orderModel->create($arr);
      if ($ml) {
        return json_encode(['code' => 0, 'msg' => '成功']);
      }
      return json_encode(['code' => 1, 'msg' => '失败']);
    }
  }
  /**
   * 回执状态回调
   *
   * @return string
   */
  public function VisitNotify()
  {
    $data = json_decode(file_get_contents('php://input'), true);
    Log::info("==I云服回执状态回调==" . json_encode($data));
    $list = $data;
    if ($list['encryFlag'] == 'Y') {
      $list = IyunBinkService::RSA_openssl($list["data"], 'decode');
      $list=json_decode($list,true);
    }
    $orderModel = new Order();
    $orderFind = $orderModel->getFindpolicyNo($list['policyNo'], 2);
    if ($orderFind) {
      //修改
      $orderFind->orderStatus = $list['policyStatus'];
      $orderFind->returnVisitStatus = isset($list['receiptStatus ']) ? $list['receiptStatus '] : '';
      $orderFind->visitTime = isset($list['receiptTime ']) ? $list['receiptTime '] : '';
      if ($list['policyStatus'] == 3) {
        $orderFind->status = 1;
        $JobQueueName = "";
        $jobData = ['ts' => time(),'order_id'=>$orderFind->id];
        Queue::later(10, $this->JobClassName, $jobData, $JobQueueName);
      } else {
        $orderFind->status = 4;
        $orderFind->refundTime = isset($list['receiptTime ']) ? $list['receiptTime '] : '';
      }
      $orderFind->updatetime = time();
      $orderFind->save();
      return json_encode(['code' => 0, 'msg' => '成功']);
    } else {
      //新增
      return json_encode(['code' => 1, 'msg' => '失败']);
    }
  }
  /**
   * 状态回调
   *
   * @return string
   */
  public function policyNotify()
  {
    $data = json_decode(file_get_contents('php://input'), true);
    Log::info("==I云服状态回调==" . json_encode($data));
    $list = $data;
    if ($list['encryFlag'] == 'Y') {
      $list = IyunBinkService::RSA_openssl($list["data"], 'decode');
      $list=json_decode($list,true);
    }
    $orderModel = new Order();
    $orderFind = $orderModel->getFindpolicyNo($list['policyNo'], 2);
    if ($orderFind) {
      //修改
      $orderFind->orderStatus = $list['policyStatus'];
      $orderFind->effective_date = isset($list['hestiTime ']) ? $list['hestiTime '] : '';
      $orderFind->creat_date_end = isset($list['surrenderTime ']) ? $list['surrenderTime '] : '';
      if ($list['policyStatus'] == 3) {
        $orderFind->status = 1;
        $JobQueueName = "";
        $jobData = ['ts' => time(),'order_id'=>$orderFind->id];
        Queue::later(10, $this->JobClassName, $jobData, $JobQueueName);
      } else {
        $orderFind->status = 4;
        $orderFind->refundTime = isset($list['operationTime ']) ? $list['operationTime '] : '';
      }
      $orderFind->updatetime = time();
      $orderFind->save();
      return json_encode(['code' => 0, 'msg' => '成功']);
    } else {
      //新增
      return json_encode(['code' => 1, 'msg' => '失败']);
    }
  }
   /**
   * 人工核对状态回调
   *
   * @return string
   */
  public function peopleNotify()
  {
    $data = json_decode(file_get_contents('php://input'), true);
    Log::info("==I云服状态回调==" . json_encode($data));
    $list = $data;
    if ($list['encryFlag'] == 'Y') {
      $list = IyunBinkService::RSA_openssl($list["data"], 'decode');
      $list=json_decode($list,true);
    }
    $orderModel = new Order();
    $orderFind = $orderModel->getFindOrderId($list['orderId'], 2);
    if ($orderFind) {
      //修改
      $orderFind->orderStatus = $list['uwStatus'];
      // $orderFind->effective_date = isset($list['hestiTime ']) ? $list['hestiTime '] : '';
      // $orderFind->creat_date_end = isset($list['surrenderTime ']) ? $list['surrenderTime '] : '';
      if ($list['uwStatus'] == 3) {
        $orderFind->status = 1;
        $JobQueueName = "";
        $jobData = ['ts' => time(),'order_id'=>$orderFind->id];
        Queue::later(10, $this->JobClassName, $jobData, $JobQueueName);
      } else {
        $orderFind->status = 4;
        // $orderFind->refundTime = isset($list['operationTime ']) ? $list['operationTime '] : '';
      }
      $orderFind->updatetime = time();
      $orderFind->save();
      return json_encode(['code' => 0, 'msg' => '成功']);
    } else {
      //新增
      return json_encode(['code' => 1, 'msg' => '失败']);
    }
  }
}
